package com.supreme.businessserver.utils.datasource;

import com.alibaba.druid.pool.DruidDataSource;

public class LocalDatasource implements IDatasource {

    protected final static String sDriverClassName = "com.mysql.cj.jdbc.Driver";
    protected final DruidDataSource druidDataSource = new DruidDataSource();

    public LocalDatasource() {
        druidDataSource.setDriverClassName(sDriverClassName);
    }

    @Override
    public LocalDatasource setDriverClassName(String driverClassName) {
        druidDataSource.setDriverClassName(driverClassName);
        return this;
    }

    @Override
    public LocalDatasource setUrl(String host, int port, String dbName) {
        String connectUrl = String.format("jdbc:mysql://%s:%s/%s?useUnicode=true&characterEncoding=utf-8&useSSL=false", host, port, dbName);
        druidDataSource.setUrl(connectUrl);
        return this;
    }

    @Override
    public LocalDatasource setUsername(String username) {
        druidDataSource.setUsername(username);
        return this;
    }

    @Override
    public LocalDatasource setPassword(String password) {
        druidDataSource.setPassword(password);
        return this;
    }

    @Override
    public DruidDataSource getDatasource() {
        return druidDataSource;
    }
}
